const app = getApp();
const db = wx.cloud.database().collection("actor");
const db2 = wx.cloud.database().collection("user");
Page({
  data: {
    value: '',
    equipmentList: [] ,
    enable: false,
    actionText: '',
    searchResult: [], // 查询结果
    loadingProps: {
      size: '50rpx',
    },
  },
  onLoad: function() {
    this.getEquipmentData(); // 在页面加载时调用获取设备信息的函数
  },
  goToSearch: function() {
    wx.navigateTo({
      url: '/pages/search/search' 
    });
  },
  handleClick(e) {
    console.log('User openid:', app.globalData.user_openid);
    
    if (app.globalData.user_openid === '') {
      wx.switchTab({
        url: '/pages/my/my'
      });
      return;
    }
    
    // Query the user collection for the authen field
    db2.where({
      _openid: app.globalData.user_openid,
    }).get().then(res => {  
      if (res.data.length === 0) {
        console.error('No user found with the provided openid');
        wx.showToast({
          title: '获取用户信息失败',
          icon: 'none'
        });
        return;
      }
      
      const authen = res.data[0].authen;
      console.log('User authen field:', authen);
      
      if (authen === 2) {
        wx.navigateTo({
          url: '/pages/film-YY-up/film-YY-up'
        });
      } else {
        wx.switchTab({
          url: '/pages/my/my'
        });
        wx.showToast({
          title: '请完成身份认证',
          icon: 'error'
        });
      }
    }).catch(err => {
      console.error('Failed to get user authen:', err);
      wx.showToast({
        title: '获取用户信息失败',
        icon: 'none'
      });
    });
  },
  
 
  viewDetail(e) {
   
    const index = e.currentTarget.dataset.index; // 获取点击的<c-filmcard>的索引
    console.log(index)
    const selectedItem = this.data.equipmentList[index]; // 获取对应索引的设备信息
    
    // 检查selectedItem是否为undefined或null
    if (!selectedItem) {
      console.error("Selected item is undefined or null");
      return;
    }
    
    console.log(selectedItem._id)
    // 跳转到详细信息页面，并传递选定的设备信息
    wx.navigateTo({
      url: '/pages/film-YY-detailed/film-YY-detailed?id=' + selectedItem._id
    });
  },
  // 获取设备信息的函数
  getEquipmentData: function() {
    // 从 Cloud 数据库中获取 equipment 表中的数据
    db.get().then(res => {
      console.log(res.data); // 确保获取到数据
  
      this.setData({
        equipmentList: res.data // 将获取的数据设置到页面的数据属性中
      });
    }).catch(err => {
      console.error(err);
    });
  },
  ready() {
    this.setData({ enable: true });
    setTimeout(() => {
      this.setData({ enable: false });
    }, 1000);
  },
  onRefresh() {
    this.setData({ enable: true });
    this.getEquipmentData();
    setTimeout(() => {
      this.setData({ enable: false });
    }, 1500); 
  }
  ,
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({ scrollTop });
  },
 
  // 执行搜索
  search() {
    this.setData({
      actionText: '',
    });
    this.hideSearchResult()
    const keyword = this.data.value.trim(); // 获取用户输入的内容并去除两端空格
    console.log(keyword)
    if (!keyword) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      });
      return;
    }

    // 执行模糊查询
    db.where({
      // title: db.RegExp({
      //   regexp: keyword, // 正则表达式，用于模糊查询
      //   options: 'i' // 'i' 表示忽略大小写
      // })
      // title:keyword
      name:this.data.value
    }).get().then(res => {
      if (res.data.length === 0) {
        // Display message for no results found
        wx.showToast({
          title: '查询不到结果',
          icon: 'error'
        });
      } else {
      this.setData({
        searchResult: res.data // 更新查询结果
      });
    }
    }).catch(err => {
      console.error('查询失败', err);
      wx.showToast({
        title: '查询失败，请重试',
        icon: 'none'
      });
    });
  },


    focusHandle() {
      this.setData({
        actionText: '搜索',
      });
    },

    blurHandle() {
      this.setData({
        actionText: '',
      });
    },

    actionHandle() {
      this.search()
      
    },
    hideSearchResult() {
      this.setData({
        searchResult: [] 
      });
    }
    
});
